What is the difference between static and final keywords in Java?
149
18-Jul-2024
Updated on 18-Jul-2024
Ravi Vishwakarma
18-Jul-2024The
static
andfinal
keywords serve distinct purposes and are used in different contexts.Here's a detailed explanation of the differences between them:
static
KeywordThe
static
keyword is used to indicate that a member (variable, method, or nested class) belongs to the class itself, rather than to instances of the class.final
KeywordThe
final
keyword is used to restrict the modification of variables, methods, and classes.Summary of Differences:
static
final
Example Combining Both Keywords:
You can use both
static
andfinal
together for class constants:In this example,
MAX_USERS
andAPP_NAME
are constants that are shared across all instances of the class and cannot be modified.Read more - Java program to find all pairs of elements in an array whose sum is equal to a given number.